<template>
  <div class="login">
    <h1>登录未来校招</h1>
    <div class="tab">
      <p
          class="tab-item tab-pwd"
          :class="[active === 0 && 'active']"
          @click="tab(0)"
      >
        账号密码登录
      </p>
      <p
          class="tab-item tab-code"
          :class="[active === 1 && 'active']"
          @click="tab(1)"
      >
        验证码登录
      </p>
    </div>
    <div class="tab-current" v-if="active === 0">
      <div class="login-wrap login-pwd">
        <div class="phone clearfix">
          <p>+0086</p>
          <input v-model="mobile" name="mobile" type="tel" placeholder="请输入手机号"/>
        </div>
        <div class="pwd">
          <input
              v-model="password"
              name="password"
              @keyup.enter="submit"
              :type="activePwd ? 'text' : 'password'"
              placeholder="请输入密码"
          />
          <i
              class="iconfont"
              :class="[activePwd ? 'icon-eye-open' : 'icon-eye-close']"
              @click="activePwd = !activePwd"
          ></i>
        </div>
      </div>
    </div>
    <div class="tab-current" v-else>
      <div class="login-wrap login-code">
        <div class="phone clearfix">
          <p>+0086</p>
          <input v-model="mobile" name="mobile" type="tel" placeholder="请输入手机号"/>
        </div>
        <div class="code">
          <input
              v-model="msgCode"
              @keyup.enter="submit"
              type="text"
              name="code"
              placeholder="请输入验证码"
          />
          <MsgCode :phone-number="mobile"/>
        </div>
      </div>
    </div>
    <label class="operate" @click="toCheck">
        <span class="checkbox" v-show="checked">
          <i class="iconfont icon-hook"></i>
        </span>
      <span class="checkbox" v-show="!checked">&nbsp;</span>
      <span class="text">下次自动登录</span>
    </label>
    <div class="submit">
      <button type="button" @click="submit">登录</button>
    </div>
    <p class="notice toRegister" @click="toRegister">没有账号 <span>立即注册</span></p>
    <p class="notice toAgreement" @click="jumpTo">登录即视为您同意 <span>《未来校招用户服务协议》</span></p>
  </div>
</template>

<script>
import { msgLogin, pwdLogin } from '@/api/users'
import MsgCode from '@/components/MsgCode/MsgCode'
import { setUserInfo } from '@/utils/cookie'

export default {
  components: {
    MsgCode,
  },
  data () {
    return {
      active: 0,
      mobile: '',
      password: '',
      msgCode: '',
      showCodeBtn: true,
      count: null,
      checked: false,
      activePwd: false,
    }
  },
  created () {
  },
  mounted () {
  },
  methods: {
    tab (active) {
      this.active = active
    },
    toCheck () {
      this.checked = !this.checked
    },
    toRegister () { // 注册
      this.$router.push({
        path: 'explain'
      })
    },
    setCountDown () { // 倒计时
      this.count = 60
      this.showCodeBtn = false
      let timer = setInterval(() => {
        this.count--
        if (this.count === 0) {
          this.showCodeBtn = true
          this.count = null
          clearInterval(timer)
          timer = null
        }
      }, 1000)
    },
    loginByPwd (mobile, password) { // 密码登录
      pwdLogin({
        mobile,
        password,
        autologin: this.checked ? 1 : 0,
      }).then(res => {
        this.handelLogin(res)
      }).catch(err => {
        console.log(err)
        this.$alertErr(err || '网络错误, 稍后再试！')
      })
    },
    loginByMsg (mobile, code) { // 短信验证码登录
      msgLogin({
        mobile,
        code,
        autologin: this.checked ? 1 : 0,
      }).then(res => {
        this.handelLogin(res)
      }).catch(err => {
        console.log(err)
        this.$alertErr(err || '网络错误, 稍后再试！')
      })
    },
    handelLogin (res) { // 处理登录
      if (res.code === 200 || res.code === 1003 || res.code === 1004) {
        this.$alertSuc(res.msg || '登录成功！')
        this.$root.userInfo = res.data;
        this.toSetCookie(res.data)
        this.$store.dispatch('userInfo', res.data)
        if(this.$route.query.link) {
          this.$router.replace(this.$route.query.link)
        } else {
          this.$router.replace('/')
        }
      } else {
        this.$alertErr(res.msg || '网络错误, 稍后再试！')
      }
    },
    submit () { // 登录
      const mobile = this.mobile || ''
      const password = this.password || ''
      const msgCode = this.msgCode || ''
      if (!mobile || !/^1[3456789]\d{9}$/.test(mobile)) {
        this.$alertWarn('请正确填写手机号码！')
        return false
      }
      if (this.active === 0) { // 密码登录
        if (!password) {
          this.$alertWarn('请填写密码！')
          return false
        }
        this.loginByPwd(mobile, password)
      } else if (this.active === 1) { // 短信验证码登录
        if (!msgCode) {
          this.$alertWarn('请填写短信验证码！')
          return false
        }
        this.loginByMsg(mobile, msgCode)
      }
    },
    toSetCookie (data) {
      data.checked = this.checked
      setUserInfo(data, this.checked ? 30 : 1)
    },
    jumpTo(){
      this.$router.push({
        path: 'agreement'
      })
    },
  },
}
</script>
<style lang="stylus" scoped>
.login {
  padding: 50px 20px 0 20px;

  h1 {
    height 40px
    line-height 40px
    font-size 30px
    color $color-main
    text-align center
  }

  .tab {
    margin-top: 40px;
    padding: 0 10px;
    height: 50px;

    p {
      line-height: 49px;
      width: 120px;
      text-align: center;
      font-size: 18px;
      color: #333;
      border-bottom: 1px solid transparent;

      &.active {
        border-color: $color-main;
        color: $color-main;
      }

      &.tab-pwd {
        float: left;
      }

      &.tab-code {
        float: right;
      }
    }
  }

  .login-wrap {
    margin: 30px 0;

    .phone,
    .pwd,
    .code {
      height: 55px;
      border-bottom: 1px solid #eee;

      input {
        border: 0;
      }

      input,
      p {
        line-height: 54px;
        height: 54px;
        color: #333;
        font-size: 16px;
      }
    }

    .code {
      position: relative;
    }

    .pwd {
      position: relative;

      i {
        position: absolute;
        right: 0;
        top: 5px;
        font-size: 25px;
        padding: 10px;
        color: #999;

        &:hover {
          cursor: pointer;
          color: #333;
        }
      }
    }

    .phone {
      p {
        float: left;
        width: 80px;
      }
    }
  }

  .submit {
    margin-top: 40px;
    text-align: center;

    button {
      height: 45px;
      width: 335px;
      margin: 0 auto;
      background: $color-main;
      color: #fff;
      border: 1px solid $color-main;
      border-radius: 22.5px;
      font-size: 16px;
    }
  }

  .operate {
    display block
    line-height 26px
    span {
      display: inline-block;
      vertical-align middle
    }
    .checkbox {
      height: 22px;
      line-height: 21px;
      width: 22px;
      border: 1px solid #d8d8d8;
      border-radius: 50%;
      text-align: center;

      i {
        vertical-align: text-bottom;
        color: $color-main;
        font-size: 20px;
        font-weight: 600;
      }
    }

    .text {
      font-size: 16px;
      color: #999;
      margin-left: 10px;
    }
  }
  .toRegister {
    margin: 30px 0 40px 0;
  }
  .notice {
    color: #999;
    text-align center
    font-size 14px
    span {
      color: $color-main;
      margin-left: 5px;
    }
  }
  .toAgreement {
    margin-bottom: 40px;
    font-size: 12px;
  }
}
</style>
